﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Configuration;
using System.Windows.Controls.WpfPropertyGrid;
using devtm.Editor.Configuration.MyEditors;
using Sytem.Windows.Controls.WpfPropertyGrid.Metadata;

namespace devtm.Editor.Configuration
{
    public class PropertiesSection : INotifyPropertyChanged
    {


        private SectionInformation _infos;
        //private ElementInformation _infos2;
        private ConfigurationSection Section;

        public PropertiesSection(ConfigurationSection section)
        {
            Section = section;
            _infos = section.SectionInformation;

        }


        public ConfigurationSection ConfigurationSection()
        {
            return Section;
        }


        /// <summary>
        /// Gets or sets the config source.
        /// Gets or sets the name of the include file in which the associated configuration
        /// section is defined, if such a file exists.
        /// </summary>
        /// <value>The config source.</value>
        /// <Returns>
        /// The name of the include file in which the associated System.Configuration.ConfigurationSection
        /// is defined, if such a file exists; otherwise, an empty string ("").
        /// </Returns>
        [DisplayName("Configuration Source")]
        [Description("name of the include file in which the associated configuration")]
        [Category("Misc")]
        [PropertyOrder(0)]
        [PropertyEditor(typeof(FilePathPicker))]
        [PropertyEditorOption(typeof(FilePathPicker), "Filter:config Files (*.config)|*.config", "TypeDialog:EditorType.Save")]
        public string ConfigSource
        {
            get { return _infos.ConfigSource; }
            set { _infos.ConfigSource = value; }
        }



        /// <summary>
        /// Gets or sets a value that indicates whether the associated configuration
        /// section will be saved even if it has not been modified.
        /// </summary>
        /// <returns>
        /// true if the associated System.Configuration.ConfigurationSection object will
        /// be saved even if it has not been modified; otherwise, false. The default
        /// is false.NoteIf the configuration file is saved (even if there are no modifications),
        /// ASP.NET restarts the application.
        /// </returns>
        [DisplayName("Force Save")]
        [Description("indicates whether the associated configuration section will be saved even if it has not been modified.")]
        [Category("Misc")]
        public bool ForceSave
        {
            get { return _infos.ForceSave; }
            set 
            { 
                _infos.ForceSave = value;
                Datas.ForceSave(_infos, value);
            }
        }

      

        /// <summary>
        /// Gets the name.
        /// </summary>
        /// <value>The name.</value>
        /// <returns>
        /// The complete name of the configuration section.
        /// </returns>
        [DisplayName("Name")]
        [Category("Misc")]
        public string Name
        { get { return _infos.Name; } }



        /// <summary>
        /// Gets the name of the associated configuration section.
        /// The name of the associated System.Configuration.ConfigurationSection object.
        /// </summary>
        /// <value>The name of the section.</value>
        [DisplayName("Section Name")]
        [Category("Misc")]
        public string SectionName { get { return _infos.SectionName; } }

        

        /// <summary>
        /// Gets the type of the associated System.Configuration.ConfigurationElement object.
        /// </summary>
        /// <value>The type.</value>
        /// <returns>
        /// The type of the associated System.Configuration.ConfigurationElement object.
        /// </returns>
        [Description("Gets the type of the associated System.Configuration.ConfigurationSection object.")]
        [DisplayName("Type")]
        [Category("Object")]
        //[Designer(typeof(string), typeof(string))]
        [ExtenderProvidedProperty()]
        //[ProvideProperty("HelpText",typeof(string))]
        public Type Type { get { return _infos.GetType(); } }


        [Description("Gets the protected configuration provider for the associated configuration section.")]
        [DisplayName("Protection Provider")]
        [Category("Misc")]
        [PropertyEditor(typeof(ProtectPicker))]
        public string ProtectionProvider 
        { 
            get 
            {
                if (_infos.ProtectionProvider != null)
                    return _infos.ProtectionProvider.Name;

                return "Unprotected";
            }
            set
            {

                if (value == "Unprotected")
                {
                    if (_infos.ProtectionProvider != null)
                    {
                        _infos.UnprotectSection();
                        Datas.ForceSave(_infos, true);
                    }
                }
                else 
                {
                    if (_infos.ProtectionProvider == null)
                    {
                        _infos.ProtectSection(value);
                        Datas.ForceSave(_infos, true);
                    }
                }

                OnPropertyChanged("ProtectionProvider");

            }
        }

        //
        // Summary:
        //     Marks a configuration section for protection.
        //
        // Parameters:
        //   protectionProvider:
        //     The name of the protection provider to use.
        //
        // Exceptions:
        //   System.InvalidOperationException:
        //     The System.Configuration.SectionInformation.AllowLocation property is set
        //     to false.- or -The target section is already a protected data section.
        //public void ProtectSection(string protectionProvider);
        //
        //public void UnprotectSection();


        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string propertyName)
        {
            var handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion

    }
}
